package action.info;

import javax.servlet.http.HttpServletRequest;

import net.fckeditor.FCKeditor;

import org.apache.struts2.interceptor.ServletRequestAware;

import persistence.model.TdfkfaBulletin;
import service.BulletinService;

import com.opensymphony.xwork2.ActionSupport;

public class BulletinManageAction extends ActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = -158349609241512691L;

    private HttpServletRequest request;

    private BulletinService bulletinService;

    private String[] bulletinId;

    private String editorHtml;

    private String editorInstanceName;

    private String bulletinTitle;

    private String bid;

    private String publishState;

    public String deleteBulletin() throws Exception {

        if (bulletinId == null || bulletinId.length == 0) {
            return INPUT;
        }

        Integer[] ids = new Integer[bulletinId.length];
        for (int i = 0; i < bulletinId.length; i++) {
            ids[i] = new Integer(bulletinId[i]);
        }

        bulletinService.deleteBulletin(ids);

        return SUCCESS;
    }

    public String editBulletin() throws Exception {
        if (bulletinId == null || bulletinId.length != 1) {
            return INPUT;
        } else {
            bid = bulletinId[0];
            request.getSession().setAttribute("bid", bid);
            return SUCCESS;
        }
    }

    public String prepareEdit() throws Exception {

        editorInstanceName = "bulletinEditor";

        FCKeditor editor = new FCKeditor(request, "bulletinEditor");
        editor.setToolbarSet("Bulletin");
        editor.setHeight("400px");

        bid = (String) request.getSession().getAttribute("bid");
        TdfkfaBulletin bulletin = bulletinService.getBulletin(new Integer(bid));

        if (bulletin == null) {
            return INPUT;
        }

        editor.setValue(bulletin.getContent());

        editorHtml = editor.createHtml();

        bulletinTitle = bulletin.getTitle();
        
        return SUCCESS;
    }

    public void setBulletinId(String[] bulletinId) {
        this.bulletinId = bulletinId;
    }

    public String getEditorHtml() {
        return editorHtml;
    }

    public String getEditorInstanceName() {
        return editorInstanceName;
    }

    public String getBulletinTitle() {
        return bulletinTitle;
    }

    public String getBid() {
        return bid;
    }

    public String getPublishState() {
        return publishState;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setBulletinService(BulletinService bulletinService) {
        this.bulletinService = bulletinService;
    }

}
